/* tslint:disable:one-line:check-open-brace*/
import { OpaqueToken } from '@angular/core';

export const TITLE = new OpaqueToken('title');

import { Component, Inject } from '@angular/core';

import { DateLoggerService, MinimalLogger } from './date-logger.service';
import { Hero }              from './hero';
import { HeroService }       from './hero.service';
import { LoggerService }     from './logger.service';
import { RUNNERS_UP, runnersUpFactory }  from './runners-up';

const someHero = new Hero(42, 'Magma', 'Had a great month!', '555-555-5555');

const template = `
  <h3>{{title}}</h3>
  <div>Winner: <strong>{{heroOfTheMonth.name}}</strong></div>
  <div>Reason for award: <strong>{{heroOfTheMonth.description}}</strong></div>
  <div>Runners-up: <strong id="rups1">{{runnersUp}}</strong></div>

  <p>Logs:</p>
  <div id="logs">
    <div *ngFor="let log of logs">{{log}}</div>
  </div>
  `;

@Component({
  selector: 'hero-of-the-month',
  template: template,
  providers: [
    // provide 对象
    // useValue - 值 - 提供商
    // useClass - 类 - 提供商
    // useExisting - 别名 - 提供商
    // useFactory - 工厂 - 提供商
    { provide: Hero, useValue: someHero },
    { provide: TITLE, useValue: 'Hero of the Month' },
    /**
     * 第一个提供商是 展开了语法糖的 ，是一个典型情况的展开。一般来说，被新建的类 (HeroService) 
     * 同时也是该提供商的注入令牌。 这里用完整形态来编写它，来反衬我们更喜欢的缩写形式。
     */
    { provide: HeroService, useClass: HeroService },
    /**
     * 第二个提供商使用 DateLoggerService 来满足 LoggerService 。该 LoggerService 
     * 在 AppComponent 级别已经被注册。当 这个组件 要求 LoggerService 的时候，它得到的却
     * 是 DateLoggerService 服务。
     */
    { provide: LoggerService, useClass: DateLoggerService },
    { provide: MinimalLogger, useExisting: LoggerService },
    { provide: RUNNERS_UP, useFactory: runnersUpFactory(2), deps: [Hero, HeroService] }
  ]
})
export class HeroOfTheMonthComponent {
  logs: string[] = [];

  constructor(
    logger: MinimalLogger,
    public heroOfTheMonth: Hero,
    @Inject(RUNNERS_UP) public runnersUp: string,
    @Inject(TITLE) public title: string
    ) {
      this.logs = logger.logs;
      logger.logInfo('starting up');
  }
}
